package com.xbh.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.nimbusds.jose.JOSEException;
import com.xbh.common.web.core.Result;
import com.xbh.management.exception.JwtSignatureVerifyException;
import com.xbh.management.service.UserRoleService;
import com.xbh.management.utils.JwtUtilManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;


/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-09-25 17:09
 */

@RestController
@RequestMapping(path = "/userrole")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;


    /**
     * 给用户添加关联角色
     * @param params
     * @return
     * @throws ParseException
     * @throws JOSEException
     * @throws JwtSignatureVerifyException
     */
    @PostMapping("/addUserRelationRole")
    public Result saveUserReletionRole(@RequestBody JSONObject params) throws ParseException, JOSEException, JwtSignatureVerifyException {
        String roleIds = params.getString("roleIds");
        Long userId = params.getLong("userId");
        String username = JwtUtilManagement.getUsername();
        Date time=new Date();
        Integer integer = new Integer(0);
        int isFlag = userRoleService.insertUserRoleByIds(roleIds, userId, username, username,time,time,integer);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("保存失败");
    }

    /**
     * 给角色添加关联用户
     * @param params
     * @return
     * @throws ParseException
     * @throws JOSEException
     * @throws JwtSignatureVerifyException
     */
    @PostMapping("/addRoleRelationUser")
    public Result saveRoleRelationUser(@RequestBody JSONObject params) throws ParseException, JOSEException, JwtSignatureVerifyException {
        String userIds = params.getString("userIds");
        Long roleId = params.getLong("roleId");
        String username = JwtUtilManagement.getUsername();
        Date time=new Date();
        Integer integer = new Integer(0);
        int isFlag = userRoleService.insertRoleRelationUserByIds(userIds, roleId, username, username,time,time,integer);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("保存失败");
    }

    /**
     * 删除关联角色
     * @param params
     * @return
     */
    @PostMapping("/deleteUserRelationRole")
    public Result delete(@RequestBody JSONObject params){
        Long userId = params.getLong("userId");
        Long roleId = params.getLong("roleId");
        int isFlag = userRoleService.deleteUserRoleByIds(userId, roleId);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("删除失败");
    }
}
